package com.gui.windows.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import com.gui.beans.*;
import com.gui.windows.*;
import com.data.*;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

/**

 * Singleton Class that manages the main window menu bar.

 * */
public class MenuBar extends JMenuBar {

 //**************************************************************************************************
 //	Constructor.
 //**************************************************************************************************

 private static MenuBar menuBar = null;

 public static MenuBar getMenuBar(MainWindow mw) {
  if (menuBar == null) {
   menuBar = new MenuBar();
   mainWindow = mw;
  }
  return menuBar;
 }

 private MenuBar () {
  super();
  createMenuBar();
 }

 //**************************************************************************************************
 //	Atributes.
 //**************************************************************************************************

 // main window menu items.
 private JMenu fileMenu; // the menus available in the menu bar.
 private JMenu editMenu;
 private JMenuItem newTemplate; // the menu item.
 private JMenuItem loadTemplate;
 private JMenuItem saveTemplate;
 private JMenuItem templateTools;
 private JMenuItem copy;
 private JMenuItem paste;
 // This static variable (mainWindow) is just used to access to the other main window components, because 
 // in some cases it is neccessary to set the enablement of those componentes according to some elections 
 // in the menu bar that is managed by this class.
 private static MainWindow mainWindow;

 //**************************************************************************************************
 //	Methods that exports this Class (Public Methods).
 //**************************************************************************************************

 //**************************************************************************************************
 //	Inner Methods. (Private Methods) 
 //**************************************************************************************************

 private void createMenuBar() {
  fileMenu = new JMenu("File");
  editMenu = new JMenu("Edit");
  createBarMenus();
  this.add(fileMenu);
  this.add(editMenu);
 }

 private void createBarMenus () {
  createMenusSubItems();
  // adding items to file drop down menu.
  fileMenu.add(newTemplate);
  fileMenu.add(loadTemplate);
  fileMenu.add(saveTemplate);
  // adding items to edit drop down menu.
  editMenu.add(copy);
  editMenu.add(paste);
  // setting events for file drop down menu items.
  newTemplate.addActionListener(new NewTemplateHandler());
  loadTemplate.addActionListener(new LoadTemplateHandler());
  saveTemplate.addActionListener(new SaveTemplateHandler());
 }

 private void createMenusSubItems () {
  newTemplate = new JMenuItem("New Template");
  loadTemplate = new JMenuItem("Load Template");
  saveTemplate = new JMenuItem("Save Template");
  copy = new JMenuItem("Copy");
  paste = new JMenuItem("Paste");
 }

 //**************************************************************************************************
 //* Components handler classes. 
 //**************************************************************************************************

    private class NewTemplateHandler implements ActionListener {
     public void actionPerformed (ActionEvent e) {
      mainWindow.getToolPanel().setEnabled(true); // enables de tools because now there is something to edit.
      mainWindow.getSelectionPanel().setEnabled(true);
      mainWindow.getStatePanel().setEnabled(true);
      mainWindow.getDesignManager().createNewComponent(IdManager.TEMP_PREFIX);
  }
 }

 /**

	 * Manage the action events of the load image entry that exist in the file drop down menu.

	 * */
 private class LoadTemplateHandler implements ActionListener {
     public void actionPerformed (ActionEvent e) {
      FileSelector fs = new XmlFileSelector(); // the templates are saved in XML files.
      File file = fs.createAndShowGUI();
   }
 }

 private class SaveTemplateHandler implements ActionListener {
     public void actionPerformed (ActionEvent e) {
      FileSelector fs = new XmlFileSelector();
      File file = fs.createAndShowGUI();
   }
 }

}
